<template>
  <div class="article-card" @click="navigateToDetail">
    <div class="article-cover" v-if="article.coverImage">
      <img :src="article.coverImage" :alt="article.title">
    </div>
    <div class="article-content">
      <h3 class="article-title">{{ article.title }}</h3>
      <p class="article-desc">{{ formatContent(article.content) }}</p>
      <div class="article-meta">
        <div class="meta-item">
          <el-avatar :size="24" :src="article.authorAvatar || defaultAvatar"></el-avatar>
          <span>{{ article.author }}</span>
        </div>
        <div class="meta-item">
          <el-icon><Clock /></el-icon>
          <span>{{ formatDate(article.createdAt) }}</span>
        </div>
        <div class="meta-item tags">
          <el-tag v-for="tag in article.tags" :key="tag" size="small" effect="plain">{{ tag }}</el-tag>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Clock } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { computed } from 'vue'

const props = defineProps({
  article: {
    type: Object,
    required: true
  }
})

const router = useRouter()
const defaultAvatar = 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'

// 导航到文章详情页
function navigateToDetail() {
  router.push(`/article/${props.article.id}`)
}

// 格式化文章内容（截取一部分作为摘要）
function formatContent(content: string) {
  if (!content) return ''
  // 移除HTML标签
  const plainText = content.replace(/<[^>]+>/g, '')
  // 截取前100个字符作为摘要
  return plainText.length > 100 ? plainText.slice(0, 100) + '...' : plainText
}

// 格式化日期
function formatDate(dateStr: string) {
  if (!dateStr) return ''
  const date = new Date(dateStr)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  })
}
</script>

<style scoped lang="scss">
.article-card {
  background-color: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s, box-shadow 0.3s;
  cursor: pointer;
  margin-bottom: 20px;
  
  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  }
  
  .article-cover {
    height: 200px;
    overflow: hidden;
    
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      transition: transform 0.3s;
    }
    
    &:hover img {
      transform: scale(1.05);
    }
  }
  
  .article-content {
    padding: 20px;
  }
  
  .article-title {
    margin: 0 0 10px;
    font-size: 18px;
    font-weight: bold;
    color: #303133;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  
  .article-desc {
    margin: 0 0 15px;
    font-size: 14px;
    color: #606266;
    line-height: 1.6;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    height: 70px;
  }
  
  .article-meta {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    font-size: 12px;
    color: #909399;
    
    .meta-item {
      display: flex;
      align-items: center;
      margin-right: 15px;
      margin-bottom: 5px;
      
      .el-icon {
        margin-right: 5px;
      }
      
      .el-avatar {
        margin-right: 5px;
      }
    }
    
    .tags {
      margin-left: auto;
      display: flex;
      flex-wrap: wrap;
      gap: 5px;
    }
  }
}
</style> 